﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyVatLieuXayDung
{
    public partial class FormTimMatHang : Form
    {
        private bool mousePressed = false;
        private Point diff = new Point(0, 0);
        public FormTimMatHang()
        {
            BitmapRegion.BitmapRegion.CreateControlRegion(this, Properties.Resources.FrmMatHang);
            InitializeComponent();
            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();
        }
        public delegate void SEND(string s,string tenDonVi);
        public SEND sender;

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                LayerDTO.DonViDTO dv = LayerBUS.DonViBUS.LayDanhSachDonViTheoMaDonVi(xuLy.ChuoiKetNoi, list[dataGridView1.SelectedRows[0].Index].MaDV);
                this.sender(dataGridView1.SelectedRows[0].Cells[1].Value.ToString(), dv.TenDV);
                this.Close();
            }
            catch (Exception ex)
            { }
        }
        LayerDTO.XuLyChuoiKetNoiDTO xuLy = new LayerDTO.XuLyChuoiKetNoiDTO();
        List<LayerDTO.MatHangDTO> list = new List<LayerDTO.MatHangDTO>();
        private void FormTimMatHang_Load(object sender, EventArgs e)
        {
            
            xuLy=LayerBUS.XuLyChuoiKetNoiBUS.DocChuoiKetNoi("connection.xml");
            comboBox1.DataSource = LayerBUS.DonViBUS.LayDanhSachDonVi(xuLy.ChuoiKetNoi);
            comboBox1.DisplayMember = "TenDV";
            comboBox1.ValueMember = "MaDV";
            list = LayerBUS.MatHangBUS.LayDanhSachMatHang(xuLy.ChuoiKetNoi);
            dataGridView1.DataSource = list;
            dataGridView1.Columns.Remove("MaDV");
            dataGridView1.Columns.Remove("DaXoa");
            dataGridView1.Columns.Remove("MaLoaiMH");
            dataGridView1.Columns.Remove("Gia");
        }

        private void FormTimMatHang_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mousePressed = true;

                Point p = new Point(e.X, e.Y);
                p = PointToScreen(p);
                diff.X = p.X - DesktopLocation.X;
                diff.Y = p.Y - DesktopLocation.Y;
            }
        }

        private void FormTimMatHang_MouseMove(object sender, MouseEventArgs e)
        {
            if (mousePressed && (e.Button & MouseButtons.Left) != 0)
            {
                Point p = new Point(e.X, e.Y);
                p = PointToScreen(p);
                p.X -= diff.X;
                p.Y -= diff.Y;
                DesktopLocation = p;
            } 
        }

        private void FormTimMatHang_MouseUp(object sender, MouseEventArgs e)
        {
            mousePressed = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            list=LayerBUS.MatHangBUS.TimMatHang(xuLy.ChuoiKetNoi, textBox1.Text);
            dataGridView1.DataSource = list;
        }

        private void textBox1_MouseDown(object sender, MouseEventArgs e)
        {
            textBox1.Text = "";
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            list = LayerBUS.MatHangBUS.TimMatHang(xuLy.ChuoiKetNoi, textBox1.Text);
            dataGridView1.DataSource = list;
            
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
            textBox2.Text = list[e.RowIndex].TenMatHang;
            textBox3.Text = list[e.RowIndex].SoLuongTon.ToString();
            LayerDTO.DonViDTO dv = LayerBUS.DonViBUS.LayDanhSachDonViTheoMaDonVi(xuLy.ChuoiKetNoi, list[e.RowIndex].MaDV);
            comboBox1.Text = dv.TenDV;
            comboBox1.SelectedValue = dv.MaDV;
                textBox4.Text = list[e.RowIndex].Gia.ToString();
            }
            catch (Exception ex)
            { }
        }
    }
}
